const fs = require('fs')
const path =require('path')
const http = require ('http')
const dayjs = require('dayjs')
const bd = require('./bd')

const servers =http.createServer()
//监听
servers.listen(8000,()=>{
    console.log('服务器启动了')
})
//监听发送请求
servers.on('request',(request,response)=>{
    const {url,method} = request
    let newsmessage = {}
    if(method.toLowerCase() === 'get'){
       
        if(url === '/api/news'){
            newsmessage=bd.news()
        }
    }
    //跨域
    response.setHeader('Access-Control-Allow-Origin', '*')
 response.setHeader('Content-Type', 'application/json; charset=utf-8')
  // 不能直接响应 对象/数组 需要转为字符串
  console.log(newsmessage)
  response.end(JSON.stringify(newsmessage))

})
